package com.btkjgc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.PropertyCompany;
import com.btkjgc.service.intf.PropertyCompanyManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Controller
public class PropertyCompanyManageController {

	static Logger logger = Logger.getLogger(PropertyCompanyManageController.class);

    @Autowired
	private PropertyCompanyManageService_Intf propertyCompanyManageService;

	@RequestMapping(value = "property_company_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,PropertyCompany propertyCompany) {

		ModelAndView mv = new ModelAndView("property_company_manage");
		getPropertyCompanyList(request, mv,propertyCompany);
		return mv;
	}

	@RequestMapping(value = "edit_property_company", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public AjaxObj editPropertyCompany(PropertyCompany propertyCompany)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {

		AjaxObj ajaxObj=propertyCompanyManageService.updatePropertyCompany(propertyCompany);

		return ajaxObj;
	}

 
	@SuppressWarnings("unchecked")
	private void getPropertyCompanyList(HttpServletRequest request, ModelAndView mv,PropertyCompany propertyCompany) {
 
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
 

		String sql ="SELECT * from property_company_t  WHERE  del = 0";

		sql = propertyCompanyManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = propertyCompanyManageService.getCount(sql, paramList);

		// 获取分页查询的数据结果
		List<PropertyCompany> propertyCompanyList = propertyCompanyManageService.getList(
				PropertyCompany.class, paramList, sql, pageIndex, 15, sortfield);

		// 设置后台用户界面所需的其他数据
		propertyCompanyManageService.setListData(request, mv, pageIndex, totalRecord,
				propertyCompanyList);

	}

	@RequestMapping(value = "get_property_company_by_id")
	@ResponseBody
	public PropertyCompany getPropertyCompanyById(Integer id) {

		String sql = "SELECT *\n"
				+ "FROM\n"
				+ "property_company_t ecst\n"
				+ "WHERE  id = ? and del=0";

		PropertyCompany propertyCompany = (PropertyCompany) HibernateUtil
				.executeSQLQueryObject(sql, PropertyCompany.class, id);

		return propertyCompany;
	}

	@RequestMapping(value = "add_property_company/{a}")
	@ResponseBody
	public   Integer jsonAddAdminUser(HttpServletRequest request,
			@PathVariable("a") int a, PropertyCompany propertyCompany) {
 
		if (propertyCompany.getName().length() > 0) {
 
			Integer reInt = propertyCompanyManageService
					.insertPropertyCompany(propertyCompany);
			return reInt;

		}
		return -1;
	}

}
